﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using FastTravel.DB;
using FastTravel.DB.Enums;
using FastTravel.DB.Helpers;
using Panel = Ext.Net.Panel;

namespace FastTravel.IntranetWeb
{
	public partial class Main : System.Web.UI.MasterPage
	{

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!HttpContext.Current.User.Identity.IsAuthenticated)
			{
				Response.Redirect(BasePage.urlLogin);
			}

			if (!IsPostBack)
			{
				var currentSessionUserDTO = (CurrentUserDTO)Session[BasePage.sessionCurrentUser];
				
				if (currentSessionUserDTO != null)
				{
					btnProfile.Text = btnProfile.Text + ": " + currentSessionUserDTO.Name + " (" +
								  EnumMetadataConverter.GetStringValue(currentSessionUserDTO.UserRole) + ")";
				}
				else
				{
					Response.Redirect(BasePage.urlLogin);
				}
			}
		}

		protected void BtnLogoutClick(object sender, DirectEventArgs e)
		{
			Session[BasePage.sessionCurrentUser] = null;
			FormsAuthentication.SignOut();
			Response.Redirect(BasePage.urlLogin);
		}

		public void AddTab(string url, string title, string loadTitle)
		{
			LoadConfig loadConfig = new LoadConfig
			                        	{
			                        		Url = url,
			                        		Mode = LoadMode.IFrame,
			                        		ShowMask = true,
			                        		MaskMsg = "Загрузка " + loadTitle + "...",
			                        		NoCache = true
			                        	};

			Panel panel = new Panel();
			panel.LoadContent(loadConfig);
			
			
			tpMain.Add(panel);
		}
	}
}